package com.huaqing.manage.service.impl;

import java.util.List;
import com.huaqing.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.huaqing.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.huaqing.manage.domain.Customer;
import com.huaqing.manage.mapper.DiningTableMapper;
import com.huaqing.manage.domain.DiningTable;
import com.huaqing.manage.service.IDiningTableService;

/**
 * 桌位管理Service业务层处理
 * 
 * @author cdutTang
 * @date 2025-02-28
 */
@Service
public class DiningTableServiceImpl implements IDiningTableService 
{
    @Autowired
    private DiningTableMapper diningTableMapper;

    /**
     * 查询桌位管理
     * 
     * @param tableId 桌位管理主键
     * @return 桌位管理
     */
    @Override
    public DiningTable selectDiningTableByTableId(Long tableId)
    {
        return diningTableMapper.selectDiningTableByTableId(tableId);
    }

    /**
     * 查询桌位管理列表
     * 
     * @param diningTable 桌位管理
     * @return 桌位管理
     */
    @Override
    public List<DiningTable> selectDiningTableList(DiningTable diningTable)
    {
        return diningTableMapper.selectDiningTableList(diningTable);
    }

    /**
     * 新增桌位管理
     * 
     * @param diningTable 桌位管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDiningTable(DiningTable diningTable)
    {
        diningTable.setCreateTime(DateUtils.getNowDate());
        int rows = diningTableMapper.insertDiningTable(diningTable);
        insertCustomer(diningTable);
        return rows;
    }

    /**
     * 修改桌位管理
     * 
     * @param diningTable 桌位管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDiningTable(DiningTable diningTable)
    {
        diningTable.setUpdateTime(DateUtils.getNowDate());
        diningTableMapper.deleteCustomerByCustomerId(diningTable.getTableId());
        insertCustomer(diningTable);
        return diningTableMapper.updateDiningTable(diningTable);
    }

    /**
     * 批量删除桌位管理
     * 
     * @param tableIds 需要删除的桌位管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDiningTableByTableIds(Long[] tableIds)
    {
        diningTableMapper.deleteCustomerByCustomerIds(tableIds);
        return diningTableMapper.deleteDiningTableByTableIds(tableIds);
    }

    /**
     * 删除桌位管理信息
     * 
     * @param tableId 桌位管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDiningTableByTableId(Long tableId)
    {
        diningTableMapper.deleteCustomerByCustomerId(tableId);
        return diningTableMapper.deleteDiningTableByTableId(tableId);
    }

    /**
     * 新增用户信息信息
     * 
     * @param diningTable 桌位管理对象
     */
    public void insertCustomer(DiningTable diningTable)
    {
        List<Customer> customerList = diningTable.getCustomerList();
        Long tableId = diningTable.getTableId();
        if (StringUtils.isNotNull(customerList))
        {
            List<Customer> list = new ArrayList<Customer>();
            for (Customer customer : customerList)
            {
                customer.setCustomerId(tableId);
                list.add(customer);
            }
            if (list.size() > 0)
            {
                diningTableMapper.batchCustomer(list);
            }
        }
    }
}
